* Figure A3 - Attrition
* Sample used to study household outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

* evolution of balanced panel
local num 0
tempfile panel 
tempfile tempdata 
save `panel'
foreach i of numlist 2005/2013 {
local num = `num' + 1
keep if mkt_year <= `i'
keep id mkt_year c_problem
bys id c_problem: gen balanced`i' = (_N ==`num') 
bys id : gen balanced_miss`i'= (_N == `num') 
save `tempdata', replace 
use `panel', replace 
merge m:1 id mkt_year using `tempdata' , nogen
save `panel', replace
}

** adding in 2009 HHs

local num 0
tempfile panel 
tempfile tempdata1 
save `panel'
foreach i of numlist 2008/2013 {
local num = `num' + 1
keep if mkt_year <= `i'
keep id mkt_year c_problem firstt_2009
bys id c_problem: gen bal09_`i' = (_N ==`num') if firstt_2009==1
bys id : gen balanced_miss09_`i'= (_N == `num') if firstt_2009==1
save `tempdata1', replace 
use `panel', replace 
merge m:1 id mkt_year using `tempdata1' , nogen
save `panel', replace
}

local num 0
foreach i of numlist 2005/2013{
egen FS_`i'= count(id) if balanced_miss`i'==1 
egen FS_miss_`i' = count(id) if balanced`i'==1
egen To_`i' = count(id) if originaltvillage==1 & balanced`i' == 1
egen Tn_`i' = count(id) if firstt_2007==1 & balanced`i' == 1
egen C_`i' = count(id) if Tvillage==0 & balanced`i' == 1
}

foreach i of numlist 2008/2013{
egen FS09_`i'= count(id) if balanced_miss09_`i'==1 & firstt_2009==1
egen FS09_miss_`i' = count(id) if bal09_`i'==1 & firstt_2009==1
egen T9_`i' = count(id) if firstt_2009==1 & bal09_`i' == 1 & firstt_2009==1
}

local num 0
foreach i of numlist 2005/2013{
local num = `num' + 1
replace FS_miss_`i' = FS_miss_`i'/`num'
replace FS_`i' = FS_`i'/`num'
replace To_`i' = To_`i'/`num'
replace Tn_`i' = Tn_`i'/`num'
replace C_`i' = C_`i'/`num'  
}

local num 0
foreach i of numlist 2008/2013{
local num = `num' + 1
replace FS09_miss_`i' = FS09_miss_`i'/`num'
replace FS09_`i' = FS09_`i'/`num'
replace T9_`i' = T9_`i'/`num'
}

forvalues i = 2005/2013 {
foreach var of varlist To_`i' Tn_`i' C_`i' FS_`i' FS_miss_`i' {
replace `var' = . if mkt_year != `i'
}
}

forvalues i = 2008/2013 {
foreach var of varlist T9_`i' FS09_`i' FS09_miss_`i' {
replace `var' = . if mkt_year != `i'
}
}

egen To = rowtotal(To_*)
egen Tn = rowtotal(Tn_*)
egen T9 = rowtotal(T9_*)
egen C = rowtotal(C_*)
egen FS = rowtotal (FS_2* )
egen FS_miss= rowtotal(FS_miss_*)
egen FS09 = rowtotal(FS09_2*)
egen FS09_miss = rowtotal(FS09_2*)

egen FS_add = max(FS09)
egen FS09_miss_add = max(FS09_miss)

replace To = . if To == 0
replace Tn = . if Tn == 0
replace C = . if C == 0
replace T9 = . if T9 == 0
replace FS=. if FS==0
replace FS_miss=. if FS_miss==0
replace FS_add = . if FS==0
replace FS09_miss_add =. if FS_miss==0

replace FS= FS+ FS_add if mkt_year > = 2008
replace FS_miss = FS_miss + FS09_miss_add if mkt_year > = 2008



lab var To "Treatment Group 1 (excluding missing outcome data)"
lab var Tn "Treatment Group 2 (excluding missing outcome data)"
lab var T9 "Treatment Expansion Group (excluding missing outcome data)"
lab var C "Control Group (excluding missing outcome data)" 
la var FS "Full Balanced Panel (including missing outcome data)"
la var FS_miss "Balanced Panel (excluding missing outcome data)"

lab var mkt_year "Year"

twoway connected FS FS_miss To Tn T9 C mkt_year, sort ytitle("Number of Respondents", size(small)) graphregion(color(white)) plotregion(icolor(white)) legend(col(1)) msymbol(T D O) ylabel(0(500)1500, labsize(small)) xlabel(2005(2)2013, labsize(small))
graph export "../output/graphs/af3a.emf", replace
